package com.sn.author.service.impl;

import com.alibaba.fastjson.JSON;
import com.sn.author.common.context.JwtTokenHelper;
import com.sn.author.common.enums.LoginStatusEnum;
import com.sn.author.common.enums.TaskTypeEnum;
import com.sn.author.common.exception.BadRequestException;
import com.sn.author.common.sdk.response.IMResponse;
import com.sn.author.common.sdk.response.LoginDyResponse;
import com.sn.author.common.sdk.response.LoginVerifyResponse;
import com.sn.author.dao.ImportAccountMapper;
import com.sn.author.dao.UserMapper;
import com.sn.author.domain.UserExample;
import com.sn.author.domain.dto.LoginDTO;
import com.sn.author.domain.entity.ImportAccount;
import com.sn.author.domain.entity.User;
import com.sn.author.domain.vo.LoginVo;
import com.sn.author.common.exception.LoginException;
import com.sn.author.filter.content.UserDetail;
import com.sn.author.service.dao.LoginService;
import com.sn.author.service.dao.PushMessageService;
import com.sn.author.utils.BeanUtils;
import com.sn.author.utils.DateUtils;
import com.sn.author.utils.SdkProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author xiaom
 * @Date 2020/9/15 20:21
 * @Version 1.0.0
 * @Description <权限验证>
 **/
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private ImportAccountMapper importAccountMapper;

    @Autowired
    private PushMessageService pushMessageService;

    @Autowired
    private SdkProcessService sdkProcessService;

    @Override
    public LoginDTO login(LoginVo loginVo) {

        User user = userMapper.selectByUserName(loginVo.getUserName());
        if(user == null || !user.getPassword().equals(loginVo.getPassword())){
            throw new LoginException("账户或密码错误");
        }
        return new LoginDTO(JwtTokenHelper.generateToken(new UserDetail(BeanUtils.transformFrom(user, UserExample.class))));
    }

    @Override
    public Boolean sendAuthCode(String code) {
        LoginVerifyResponse response = sdkProcessService.executeLoginVerify(code == null ? null : Integer.parseInt(code));
        if(response.getSuccess()){
            LoginVerifyResponse.ResponseMsgResult data = response.getData();
            if(data.getPhone() == null){
                throw new BadRequestException("验证码登录返回结果异常,phone字段为空");
            }
            ImportAccount importAccount = importAccountMapper.selectByPhone(data.getPhone());
            Assert.notNull(importAccount,"不存在的手机号码:" + data.getPhone());
            //更新用户cookie信息
            importAccount.setUrlParam(data.getUrlParam());
            importAccount.setHeader(data.getHeader());
            importAccount.setLoginStatus(LoginStatusEnum.LOGIN.getStatus());
            importAccount.setLastLoginTime(DateUtils.formatYMDHMS(new Date()));
            importAccountMapper.updateByPrimaryKeySelective(importAccount);
            log.info("验证码登录成功 手机号码:{}", data.getPhone());
            pushMessageService.pushMessage(PushMessageService.buildTaskPushMessage("验证码登录成功 手机号码:" + data.getPhone(),"-1", TaskTypeEnum.LOGIN.getType()));
        }
        return true;
    }
}
